using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using EntLibEC.Cmn;
using EntLibEC.Info.Stock;
using EntLibEC.Info.Basic;
using EntLibEC.Biz;
using EntLibEC.Biz.Basic;
using EntLibEC.Biz.Stock;

namespace EntLibEC.View.Stock
{
	/// <summary>
	/// Summary description for ShiftItemOpt.
	/// </summary>
	public partial class ShiftItemOpt : PageBase
	{

		protected EntLibEC.View.Cmn.Stock ucStockA;
		protected EntLibEC.View.Cmn.Stock ucStockB;
		protected EntLibEC.View.Cmn.StockProduct ucProduct;

		private int sysno = AppConst.IntNull;
		private int productsysno = AppConst.IntNull;
		private string opt;
		protected string bodyOnLoad="";
		private ShiftInfo oInfo;
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			//if sysno is null then new, else update
			if ( Request.QueryString["sysno"] != null)
			{
				try
				{
					this.sysno = Convert.ToInt32(Request.QueryString["sysno"].ToString());
				}
				catch
				{
					Assert(lblMessage, "sysno is not an integer", -1);
					return;
				}
			}
			else
			{
				Assert(lblMessage, "sysno missing", -1);
			}

			if ( Request.QueryString["productsysno"] != null)
			{
				try
				{
					this.productsysno = Convert.ToInt32(Request.QueryString["productsysno"].ToString());
				}
				catch
				{
					Assert(lblMessage, "productsysno is not an integer", -1);
					return;
				}
			}

			if ( Request.QueryString["opt"] == null )
			{
				Assert(lblMessage, "opt missing", -1);
				return;
			}
			else
			{
				this.opt = Request.QueryString["opt"].ToString();
			}

			InitForm();
			if ( !Page.IsPostBack)
			{
				ucStockA.BindStock(false);
				ucStockB.BindStock(false);
				GetInfo();
				BindForm();
			}
		}
		private void GetInfo()
		{
			if ( base.sInfo.sShift == null || this.sysno != base.sInfo.sShift.SysNo)
			{	
				base.sInfo.sShift = ShiftManager.GetInstance().Load(this.sysno);
			}
			oInfo = base.sInfo.sShift;
		}

		public void BindForm()
		{
			
			lblShiftNo.Text = oInfo.ShiftID;
			ucStockA.StockSysNo = oInfo.StockSysNoA;
			ucStockB.StockSysNo = oInfo.StockSysNoB;

			if ( opt == "update" )
			{
				ShiftItemInfo oItem = oInfo.itemHash[this.productsysno] as ShiftItemInfo;

				if ( oItem == null )
				{
					Assert(lblMessage, "get item information failed", -1);
				}
				else
				{
					ucProduct.ProductSysNo = oItem.ProductSysNo;
					txtShiftQty.Text = oItem.ShiftQty.ToString();
				}
			}
		}

		public void InitForm()
		{
			btnUpdateItem.Visible = false;
			btnAddItem.Visible = false;
			ucStockA.Enabled = false;
			ucStockB.Enabled = false;
			if ( opt == "update")
			{
				btnUpdateItem.Visible = true;
				ucProduct.Enabled = false;
			}
			else
			{
				btnAddItem.Visible = true;
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		private bool CheckForm()
		{
			//productsysno
			//qty > 0
			if ( ucProduct.ProductSysNo == AppConst.IntNull)
			{
				Assert(lblMessage, "product id or sysno can't be empty", -1);
				return false;
			}
			if ( txtShiftQty.Text.Trim() == "" )
			{
				Assert(lblMessage, "Qty can't be empty", -1);
				return false;
			}
			try
			{
				int qty = Convert.ToInt32(txtShiftQty.Text.Trim());
				if ( qty <=0 )
				{
					Assert(lblMessage, "Qty <= 0", -1);
					return false;
				}
			}
			catch
			{
				Assert(lblMessage, "Qty should be an integer", -1);
				return false;
			}
			return true;
		}

		protected void btnAddItem_Click(object sender, System.EventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.StShiftFillIn, false, lblMessage) )
				return;

			if (!CheckForm())
				return;
			try
			{
				GetInfo();
				ShiftItemInfo oItem = new ShiftItemInfo();
				
				oItem.ProductSysNo = ucProduct.ProductSysNo;
				oItem.ShiftSysNo = this.sysno;
				oItem.ShiftQty = Convert.ToInt32(txtShiftQty.Text.Trim());
				
				ShiftManager.GetInstance().InsertItem(oInfo, oItem);

				LogManager.GetInstance().Write(new LogInfo(oItem.SysNo, (int)AppEnum.LogType.St_Shift_Item_Insert, base.sInfo));
				bodyOnLoad = "javascript:window.opener.__doPostBack('btnRefresh','btnRefresh');window.close(); return false;";
			}
			catch(BizException exp)
			{
				Assert(lblMessage, exp.Message, -1);
			}
			catch(Exception exp)
			{
				Assert(lblMessage, "insert shift item error, see log", -1);
				ErrorLog.GetInstance().Write(exp.ToString());
			}
		}

		protected void btnUpdateItem_Click(object sender, System.EventArgs e)
		{
			if ( !base.CheckAccess((int)AppEnum.Privilege.StShiftFillIn, false, lblMessage) )
				return;

			if (!CheckForm())
				return;
			try
			{
				GetInfo();
				ShiftItemInfo newItem = new ShiftItemInfo();
				newItem.ProductSysNo = ucProduct.ProductSysNo;
				newItem.ShiftQty = Convert.ToInt32(txtShiftQty.Text.Trim());

				ShiftManager.GetInstance().UpdateItemQty(oInfo, newItem);

				LogManager.GetInstance().Write(new LogInfo(newItem.SysNo, (int)AppEnum.LogType.St_Shift_Item_Update, base.sInfo));
				bodyOnLoad = "javascript:window.opener.__doPostBack('btnRefresh','btnRefresh');window.close(); return false;";
			}
			catch(BizException exp)
			{
				Assert(lblMessage, exp.Message, -1);
			}
			catch(Exception exp)
			{
				Assert(lblMessage, "insert shift item error, see log", -1);
				ErrorLog.GetInstance().Write(exp.ToString());
			}
		}
	}
}
